﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using SegIdenPC.Entidad;
using SegIdenPC.Herramientas;

namespace SegIdenPC.Utilitarios
{
    /// <summary>
    /// Clase       : ImprimirDocumento
    /// Descripcion : Contiene la logica del formato de impresion
    /// Autor       : GS1
    /// Fecha       : 21/11/2013
    /// Empresa     : GS1 Peru
    /// </summary>
    public class ImprimirDocumento
    {
        #region --- Variables y Constantes ---
        // Formato de impresion
        private const string FORMATO_MONTO = "#0.00";
        private const string FORMATO_SIMBOLO_MONTO = "{0} {1}";
        private const string FORMATO_FECHA_DOCUMENTO = "dd/MM/yyyy";

        private Font fuentePrincipal = new Font("Arial", 10);
        private Font fuenteTotal = new Font("Arial", 8);
        private Guia guiaPrincipal = null;
        private PrintDocument printDocument = null;
        #endregion

        #region --- Constructor ---
        public ImprimirDocumento(Guia guia)
        {
            this.guiaPrincipal = guia;
            this.printDocument = new PrintDocument();
        }
        #endregion

        #region --- Eventos ---
        private void printDocument_PrintGuia(object sender, PrintPageEventArgs e)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 3;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            //string line = null;

            // Calculando el numero de lineas por pagina
            linesPerPage = e.MarginBounds.Height / this.fuentePrincipal.GetHeight(e.Graphics);
            linesPerPage -= 3;

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo Fecha de Emisión de la guia
            e.Graphics.DrawString(Convert.ToDateTime(this.guiaPrincipal.fechaEmision).ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 67,
                                  yPos,
                                  new StringFormat());

            //Si es una guia de una venta entonces se imprime la boleta o factura correspondiente.
            if (this.guiaPrincipal.venta != null)
            {
                // Imprimiendo comprobante de pago (si es que se registro la venta)
                e.Graphics.DrawString(this.guiaPrincipal.venta.serie + " - " + this.guiaPrincipal.venta.numero,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 302,
                                      yPos,
                                      new StringFormat());
            }

            // Imprimiendo Fecha de inicio del traslado - se considerará la fecha del sistema 
            e.Graphics.DrawString(DateTime.Now.ToString(FORMATO_FECHA_DOCUMENTO),
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 620,
                                  yPos,
                                  new StringFormat());

            count+=2;
            yPos = topMargin + ((float)(count+0.5) * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo informacion del punto de partida
            e.Graphics.DrawString(this.guiaPrincipal.tiendaOrigen.direccion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 18,
                                  yPos,
                                  new StringFormat());

            // Imprimiendo informacion del punto de destino
            //Si es transferencia
            if (this.guiaPrincipal.tiendaDestino != null)  e.Graphics.DrawString(this.guiaPrincipal.tiendaDestino.direccion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 404,
                                      yPos,
                                      new StringFormat());
            //Si es venta
            if (this.guiaPrincipal.venta != null) e.Graphics.DrawString(this.guiaPrincipal.venta.cliente.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 404,
                                     yPos,
                                     new StringFormat());
            //Si es prestamo
            if (this.guiaPrincipal.prestamo != null) e.Graphics.DrawString(this.guiaPrincipal.prestamo.cliente.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 404,
                                     yPos,
                                     new StringFormat());
            //Si es mantenimiento
            if (this.guiaPrincipal.mantenimiento != null) e.Graphics.DrawString(this.guiaPrincipal.mantenimiento.proveedor.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 404,
                                     yPos,
                                     new StringFormat());

            count++;
            yPos = topMargin + ((float)(count + 0.6) * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo informacion del punto de partida
            e.Graphics.DrawString(this.guiaPrincipal.tiendaOrigen.distrito,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 18,
                                  yPos,
                                  new StringFormat());
            // Imprimiendo informacion del punto de destino
            //Si es transferencia
            if (this.guiaPrincipal.tiendaDestino != null)  e.Graphics.DrawString(this.guiaPrincipal.tiendaDestino.direccion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 394,
                                      yPos,
                                      new StringFormat());
            //Si es venta
            if (this.guiaPrincipal.venta != null) e.Graphics.DrawString(this.guiaPrincipal.venta.cliente.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 394,
                                     yPos,
                                     new StringFormat());
            //Si es prestamo
            if (this.guiaPrincipal.prestamo != null) e.Graphics.DrawString(this.guiaPrincipal.prestamo.cliente.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 394,
                                     yPos,
                                     new StringFormat());
            //Si es mantenimiento
            if (this.guiaPrincipal.mantenimiento != null) e.Graphics.DrawString(this.guiaPrincipal.mantenimiento.proveedor.direccion,
                                     this.fuentePrincipal,
                                     Brushes.Black,
                                     leftMargin + 394,
                                     yPos,
                                     new StringFormat());

            count+=3;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo Nombre de Destinatario
            //Si es venta
            if (this.guiaPrincipal.venta != null) e.Graphics.DrawString(this.guiaPrincipal.venta.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 180,
                                  yPos,
                                  new StringFormat());
            //Si es prestamo
            if (this.guiaPrincipal.prestamo != null) e.Graphics.DrawString(this.guiaPrincipal.prestamo.cliente.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 180,
                                  yPos,
                                  new StringFormat());
            //Si es mantenimiento
            if (this.guiaPrincipal.mantenimiento != null) e.Graphics.DrawString(this.guiaPrincipal.mantenimiento.proveedor.razonSocial,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 180,
                                  yPos,
                                  new StringFormat());
            //Si es transferencia
            if (this.guiaPrincipal.solicitud != null) e.Graphics.DrawString("CASADESIGN EIRL",
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 180,
                                  yPos,
                                  new StringFormat());

            //marcar en la guia si es venta
            if (this.guiaPrincipal.venta != null) e.Graphics.DrawString("X",
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 435,
                                  yPos,
                                  new StringFormat());

            //marcar en la guia si es prestamo
            if (this.guiaPrincipal.prestamo != null) e.Graphics.DrawString("X",
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 642,
                                  yPos,
                                  new StringFormat());

            count++;
            yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo ID del destinatario
            //Si es venta
            if (this.guiaPrincipal.venta != null) e.Graphics.DrawString(this.guiaPrincipal.venta.cliente.nroDocumento,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 86,
                                  yPos,
                                  new StringFormat());
            //Si es prestamo
            if (this.guiaPrincipal.prestamo != null) e.Graphics.DrawString(this.guiaPrincipal.prestamo.cliente.nroDocumento,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 86,
                                  yPos,
                                  new StringFormat());
            //Si es mantenimiento
            if (this.guiaPrincipal.mantenimiento != null) e.Graphics.DrawString(this.guiaPrincipal.mantenimiento.proveedor.identificacion,
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 86,
                                  yPos,
                                  new StringFormat());
            //Si es transferencia
            if (this.guiaPrincipal.solicitud != null) e.Graphics.DrawString("20517141560",
                                  this.fuentePrincipal,
                                  Brushes.Black,
                                  leftMargin + 86,
                                  yPos,
                                  new StringFormat());

            ListaLineaGuia listaLineaDocumento = this.guiaPrincipal.listaLineaGuia;
            int numero = 0;
            int countTotales = 0;

            count += 3;
            countTotales = count;
            double acum = 7.1;
            // Imprimir detalle del documento
            while (count < linesPerPage && (numero < listaLineaDocumento.size()))
            {
                if (numero == 0) yPos = topMargin + (count * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 1) yPos = topMargin + ((float)(count - 0.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 2) yPos = topMargin + ((float)(count - 0.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 3) yPos = topMargin + ((float)(count - 1.2) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 4) yPos = topMargin + ((float)(count - 1.7) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 5) yPos = topMargin + ((float)(count - 2.2) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 6) yPos = topMargin + ((float)(count - 2.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 7) yPos = topMargin + ((float)(count - 3.6) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 8) yPos = topMargin + ((float)(count - 4.0) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 9) yPos = topMargin + ((float)(count - 4.7) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 10) yPos = topMargin + ((float)(count - 5.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 11) yPos = topMargin + ((float)(count - 5.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 12) yPos = topMargin + ((float)(count - 6.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 13) yPos = topMargin + ((float)(count - 6.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 20) yPos = topMargin + ((float)(count - 10.7) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 21) yPos = topMargin + ((float)(count - 11.3) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 22) yPos = topMargin + ((float)(count - 11.8) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 23) yPos = topMargin + ((float)(count - 12.5) * this.fuentePrincipal.GetHeight(e.Graphics));
                else if (numero == 24) yPos = topMargin + ((float)(count - 13.0) * this.fuentePrincipal.GetHeight(e.Graphics));
                else
                {
                    acum = acum + 0.5;
                    yPos = topMargin + ((float)(count - acum) * this.fuentePrincipal.GetHeight(e.Graphics));
                }

                // Escribir linea de codigo de producto proveedor
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].producto.codProductoProveedor,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin - 40,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de codigo de categoria
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].producto.nivelProducto.descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 100,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres descripcion del producto
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].producto.nivelProducto.descripcion + " - " +
                                      listaLineaDocumento.lista[numero].producto.nivelTipo.descripcion + " - " +
                                      listaLineaDocumento.lista[numero].producto.nivelMaterial.descripcion + " - " +
                                      listaLineaDocumento.lista[numero].producto.nivelColor.descripcion,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 166,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de unidad de medida 
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].producto.unidadMedida,
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin + 425,
                                      yPos,
                                      new StringFormat());

                // Escribir linea de caracteres cantidad
                e.Graphics.DrawString(listaLineaDocumento.lista[numero].cantidad.ToString(),
                                      this.fuentePrincipal,
                                      Brushes.Black,
                                      leftMargin +578,
                                      yPos,
                                      new StringFormat());

                
                count += 2;
                numero++;
            }
            // Imprimir totales de acuerdo al tipo de documento
            countTotales += 20;

            yPos = topMargin + (countTotales * this.fuentePrincipal.GetHeight(e.Graphics));

            // Imprimiendo Total
            //e.Graphics.DrawString(string.Format(FORMATO_SIMBOLO_MONTO, this.ventaPrincipal.moneda.simbolo,
            //                                                           this.ventaPrincipal.total.ToString(FORMATO_MONTO)),
            //                      this.fuentePrincipal,
            //                      Brushes.Black,
            //                      leftMargin + 605,
            //                      yPos,
            //                      new StringFormat());
        }

        #endregion

        #region --- Metodos Propios ---
        public void establecerImpresora(string direccionImpresora)
        {
            this.printDocument.PrinterSettings.PrinterName = direccionImpresora;
        }

        public void ejecutarGuia()
        {
            this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintGuia);
            this.printDocument.Print();
        }

      
        #endregion
    }
}
